using BXS.API.Models;
using Microsoft.AspNetCore.Mvc;

namespace BXS.API.Controllers
{
    /// <summary>
    /// 用户管理控制器
    /// </summary>
    public class UsersController : BaseController
    {
        // 模拟数据
        private static List<User> _users = new List<User>
        {
            new User { Id = 1, Name = "张三", Phone = "13800138001", Email = "zhangsan@example.com", Status = "active", CreateTime = "2024-01-01" },
            new User { Id = 2, Name = "李四", Phone = "13800138002", Email = "lisi@example.com", Status = "active", CreateTime = "2024-01-02" },
            new User { Id = 3, Name = "王五", Phone = "13800138003", Email = "wangwu@example.com", Status = "inactive", CreateTime = "2024-01-03" },
            new User { Id = 4, Name = "赵六", Phone = "13800138004", Email = "zhaoliu@example.com", Status = "active", CreateTime = "2024-01-04" },
            new User { Id = 5, Name = "钱七", Phone = "13800138005", Email = "qianqi@example.com", Status = "active", CreateTime = "2024-01-05" },
        };

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns>用户列表</returns>
        [HttpGet]
        public ActionResult<PagedApiResponse<User>> GetUsers([FromQuery] int page = 1, [FromQuery] int pageSize = 10)
        {
            var skip = (page - 1) * pageSize;
            var users = _users.Skip(skip).Take(pageSize).ToList();
            return PagedSuccess(users, _users.Count, page, pageSize);
        }

        /// <summary>
        /// 根据ID获取用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>用户信息</returns>
        [HttpGet("{id}")]
        public ActionResult<ApiResponse<User>> GetUser(int id)
        {
            var user = _users.FirstOrDefault(u => u.Id == id);
            if (user == null)
            {
                return Error<User>("用户不存在", 404);
            }
            return Success(user);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="request">创建用户请求</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public ActionResult<ApiResponse<User>> CreateUser([FromBody] CreateUserRequest request)
        {
            if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Phone) || string.IsNullOrEmpty(request.Email))
            {
                return Error<User>("请填写完整信息");
            }

            var newUser = new User
            {
                Id = _users.Count > 0 ? _users.Max(u => u.Id) + 1 : 1,
                Name = request.Name,
                Phone = request.Phone,
                Email = request.Email,
                Status = request.Status,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd")
            };

            _users.Add(newUser);
            return Success(newUser, "用户创建成功");
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="request">更新用户请求</param>
        /// <returns>更新结果</returns>
        [HttpPut("{id}")]
        public ActionResult<ApiResponse<User>> UpdateUser(int id, [FromBody] UpdateUserRequest request)
        {
            var user = _users.FirstOrDefault(u => u.Id == id);
            if (user == null)
            {
                return Error<User>("用户不存在", 404);
            }

            if (string.IsNullOrEmpty(request.Name) || string.IsNullOrEmpty(request.Phone) || string.IsNullOrEmpty(request.Email))
            {
                return Error<User>("请填写完整信息");
            }

            user.Name = request.Name;
            user.Phone = request.Phone;
            user.Email = request.Email;
            user.Status = request.Status;

            return Success(user, "用户更新成功");
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete("{id}")]
        public ActionResult<ApiResponse<bool>> DeleteUser(int id)
        {
            var user = _users.FirstOrDefault(u => u.Id == id);
            if (user == null)
            {
                return Error<bool>("用户不存在", 404);
            }

            _users.Remove(user);
            return Success(true, "用户删除成功");
        }
    }
} 